﻿#region

using System;
using System.Web.Caching;

#endregion

namespace BTL.Technology.Framework.Caching
{
    public class DataCache
    {
        public static object GetCache(string cacheKey)
        {
            return CachingProvider.Instance().GetItem(cacheKey);
        }

        public static void SetCache(string cacheKey, object objObject)
        {
            //TODO: Add dependemcy
            CacheDependency objDependency = null;
            SetCache(cacheKey, objObject, objDependency, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration,
                     CacheItemPriority.Normal, null);
        }


        public static void SetCache(string cacheKey, object objObject, CacheDependency objDependency,
                                    DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority,
                                    CacheItemRemovedCallback onRemoveCallback)
        {
            if (objObject != null)
            {
                // if no OnRemoveCallback value is specified, use the default method
                if (onRemoveCallback == null)
                {
                    onRemoveCallback = ItemRemovedCallback;
                }
                CachingProvider.Instance().Insert(cacheKey, objObject, objDependency, absoluteExpiration,
                                                  slidingExpiration, priority, onRemoveCallback);
            }
        }

        internal static void ItemRemovedCallback(string key, object value, CacheItemRemovedReason removedReason)
        {
            //TODO: if the item was removed from the cache, log the key and reason to the event log
            //try
            //{
            //    if (Globals.Status == Globals.UpgradeStatus.None)
            //    {
            //        LogInfo objEventLogInfo = new LogInfo();
            //        switch (removedReason)
            //        {
            //            case CacheItemRemovedReason.Removed:
            //                objEventLogInfo.LogTypeKey = EventLogController.EventLogType.CACHE_REMOVED.ToString();
            //                break;
            //            case CacheItemRemovedReason.Expired:
            //                objEventLogInfo.LogTypeKey = EventLogController.EventLogType.CACHE_EXPIRED.ToString();
            //                break;
            //            case CacheItemRemovedReason.Underused:
            //                objEventLogInfo.LogTypeKey = EventLogController.EventLogType.CACHE_UNDERUSED.ToString();
            //                break;
            //            case CacheItemRemovedReason.DependencyChanged:
            //                objEventLogInfo.LogTypeKey = EventLogController.EventLogType.CACHE_DEPENDENCYCHANGED.ToString();
            //                break;
            //        }
            //        objEventLogInfo.LogProperties.Add(new LogDetailInfo(key, removedReason.ToString()));
            //        EventLogController objEventLog = new EventLogController();
            //        objEventLog.AddLog(objEventLogInfo);
            //    }
            //}
            //catch (Exception ex)
            //{
            //    //Swallow exception
            //}
        }

        public static void RemoveCache(string key)
        {
            CachingProvider.Instance().RemoveCache(key);
        }
    }
}